package hhm.android.main.shoeSizeHistory;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;

import java.util.ArrayList;

import hhm.android.library.utils.OnMultiClickListener;
import hhm.android.main.R;
import siau.android.http.model.ListHistoryModel;
import siau.android.http.model.ListHistoryModelGroup;

/**
 * 这是普通的分组Adapter 每一个组都有头部、尾部和子项。
 */
public class ShoeSizeHistoryListOutAdapter extends GroupedRecyclerViewAdapter {

    protected ArrayList<ListHistoryModelGroup> mGroups;
    public int pageState;
    private OnMultiClickListener selectDataListener;
    private View.OnClickListener itemCLick;
    private View.OnClickListener deleteListener;

    public ShoeSizeHistoryListOutAdapter(Context context, ArrayList<ListHistoryModelGroup> groups, OnMultiClickListener selectDataListener, View.OnClickListener itemCLick, View.OnClickListener deleteListener) {
        super(context);
        mGroups = groups;
        this.selectDataListener = selectDataListener;
        this.itemCLick = itemCLick;
        this.deleteListener = deleteListener;
    }

    @Override
    public int getGroupCount() {
        return mGroups == null ? 0 : mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<ListHistoryModel> children = mGroups.get(groupPosition).modelList;
        return children == null ? 0 : children.size();
    }

    public void clear() {
        mGroups.clear();
        notifyDataChanged();
    }

    public void setGroups(ArrayList<ListHistoryModelGroup> groups) {
        mGroups = groups;
        notifyDataChanged();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_shoe_size_history_list_group_header;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return R.layout.item_shoe_size_history_list_group_header;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_shoe_size_history_list_group;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        ListHistoryModelGroup entity = mGroups.get(groupPosition);
        holder.setText(R.id.item_shoe_size_history_list_group_header_time, entity.date);
        View view = holder.get(R.id.item_shoe_size_history_list_group_header_ll);
        view.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (selectDataListener != null) {
                    selectDataListener.onMultiClick(v);
                }
            }
        });
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        ListHistoryModel entity = mGroups.get(groupPosition).modelList.get(childPosition);
        holder.setText(R.id.item_shoe_size_history_list_group_time, entity.getDate());
        RecyclerView recyclerView = holder.get(R.id.item_shoe_size_history_list_group_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(new ShoeSizeHistoryListInnerAdapter(mContext, entity.getRecords(), pageState, childPosition == mGroups.get(groupPosition).modelList.size() - 1, itemCLick, deleteListener));
    }
}
